package com.ttdc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ttdc.mapper.DishesMapper;
import com.ttdc.pojo.Dishes;
import com.ttdc.pojo.DishesExample;
import com.ttdc.service.DishesService;

@Controller
public class DishesController {
	@Autowired
	private DishesService dishesService;
	private DishesExample example;
	@RequestMapping("/findAll")
	public String findAllDishes(Model model){
		List<Dishes> list = dishesService.selectByAll();
		model.addAttribute("list",list);
		return "dishes/Dishestest";
	}
	@SuppressWarnings("finally")
	@RequestMapping("/saveDishes")
	public String saveDishes(Dishes record,Model model){
		try{
		dishesService.insert(record);
		List<Dishes> list = dishesService.selectByAll();
		model.addAttribute("list", list);
		}catch (Exception e){
			e.printStackTrace();
		}finally{
		return "redirect:/findAll";
		}
	}
	@RequestMapping("/deleteDishes")
	public String removeDishes(@RequestParam("cpId") Integer cpId,Model model){
		dishesService.deleteByPrimaryKey(cpId);
		return "redirect:/findAll";
	}
	@RequestMapping("/tupdateDishes")
	public String updateByPrimaryKeySelective(Integer cpId,Model model){
		Dishes cp = dishesService.selectByPrimaryKey(cpId);
		model.addAttribute("cp", cp);
		return "dishes/Dishesupdate";
	}
	@RequestMapping("/updateDishes")
	public String replaceDishes(Dishes record){
		dishesService.updateByPrimaryKey(record);
		return "redirect:/findAll";
	}
	//查询  可多條件查詢
	@RequestMapping("/selectByCpName")
	public String selectByPrimaryKey(Dishes dishes,Model model){
		example = new DishesExample();
		DishesExample.Criteria c = example.createCriteria();
		if(dishes == null){
			return "dishes/Dishestest";
		}else{
			if(dishes.getCpName()!=null){
				System.out.println(dishes.getCpName());
				c.andCpNameEqualTo(dishes.getCpName());
				List<Dishes> list = dishesService.selectByExample(example);
				model.addAttribute("list", list);
			}
			return "dishes/Dishestest";
		}
	}
	@RequestMapping("/selectAllCpMag")
	public String selectAll(Model model){
		example = new DishesExample();
		DishesExample.Criteria criteria = example.createCriteria();
		criteria.andCpIdIsNotNull();
		List<Dishes> list = dishesService.selectByExample(example);
		model.addAttribute("list", list);
		return "dishes/Dishestest";
	}
	
	
	@RequestMapping("/diancan")
	//
	public String diancan(Integer cxId,Model model){
		example=new DishesExample();
		DishesExample.Criteria criteria = example.createCriteria();
		criteria.andCxIdEqualTo(cxId);
		List<Dishes> list = dishesService.selectByExample(example);
		System.out.println(list);
		model.addAttribute("list",list);
		return "dc";
	}

}
